package com.zry.traced.config

import android.annotation.SuppressLint
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.os.Environment
import com.jq.ui.DemoApplication
import com.zry.traced.greendao.gen.DaoMaster
import com.zry.traced.greendao.gen.DaoSession
import com.zry.traced.retrofit.persistentcookiejar.ClearableCookieJar
import com.zry.traced.retrofit.persistentcookiejar.PersistentCookieJar
import com.zry.traced.retrofit.persistentcookiejar.cache.SetCookieCache
import com.zry.traced.retrofit.persistentcookiejar.persistence.SharedPrefsCookiePersistor

import java.util.concurrent.ExecutorService
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit


class MyApplication : DemoApplication() {
    /**
     * 线程池
     */
    val cachedThreadPool: ExecutorService = ThreadPoolExecutor(8, 8, 0L,
            TimeUnit.MILLISECONDS, LinkedBlockingQueue(100000),
            ThreadPoolExecutor.CallerRunsPolicy())

    /**
     * 图片保存路径
     */
    val photoSavePath = Environment.getExternalStorageDirectory().absolutePath + "/jianguan/image/"

    /**
     * 视频保存路径
     */
    val videoSavePath = Environment.getExternalStorageDirectory().absolutePath + "/jianguan/video/"

    /**
     * 音频文件保存路径
     */
    val audioSavePath = Environment.getExternalStorageDirectory().absolutePath + "/jianguan/audio/"

    /**
     * 文件保存路径
     */
    val apkSavePath = Environment.getExternalStorageDirectory().absolutePath + "/jianguan/"

    /**
     * 配置缓存
     */
    lateinit var cookieJar: ClearableCookieJar



    companion object {
        @SuppressLint("StaticFieldLeak")
        var instance: MyApplication? = null

        fun instance() = instance!!


        @SuppressLint("StaticFieldLeak")
        private var mContext: Context? = null

        fun mContext() = mContext!!
    }


    override fun onCreate() {
        super.onCreate()

        mContext = applicationContext

        instance = this
        cookieJar = PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(instance))



        setDatabase()
    }


    /**
     * 设置GreenDao
     */
    private var mHelper: DaoMaster.DevOpenHelper? = null
    private var db: SQLiteDatabase? = null
    private var mDaoMaster: DaoMaster? = null
    private var mDaoSession: DaoSession? = null


    /**
     * 设置greenDao
     */
    private fun setDatabase() {
        // 通过 DaoMaster 的内部类 DevOpenHelper，你可以得到一个便利的 SQLiteOpenHelper 对象。
        // 可能你已经注意到了，你并不需要去编写「CREATE TABLE」这样的 SQL 语句，因为 greenDAO 已经帮你做了。
        // 注意：默认的 DaoMaster.DevOpenHelper 会在数据库升级时，删除所有的表，意味着这将导致数据的丢失。
        // 所以，在正式的项目中，你还应该做一层封装，来实现数据库的安全升级。
        mHelper = DaoMaster.DevOpenHelper(this, "collec-storage-db", null)
        db = mHelper?.writableDatabase
        // 注意：该数据库连接属于 DaoMaster，所以多个 Session 指的是相同的数据库连接。
        mDaoMaster = DaoMaster(db)
        mDaoSession = mDaoMaster!!.newSession()
    }


    fun getDaoSession(): DaoSession? {
        return mDaoSession
    }


}
